import '@univerjs/presets/lib/styles/preset-sheets-core.css';
import '@univerjs/presets/lib/styles/preset-sheets-advanced.css';
import '@univerjs/presets/lib/styles/preset-sheets-filter.css';
import '@univerjs/presets/lib/styles/preset-sheets-collaboration.css';
import '@univerjs/presets/lib/styles/preset-sheets-thread-comment.css';
import '@univerjs/presets/lib/styles/preset-sheets-conditional-formatting.css';
import '@univerjs/presets/lib/styles/preset-sheets-data-validation.css';
import '@univerjs/presets/lib/styles/preset-sheets-drawing.css';
import '@univerjs/presets/lib/styles/preset-sheets-find-replace.css';
import '@univerjs/presets/lib/styles/preset-sheets-hyper-link.css';
import '@univerjs/presets/lib/styles/preset-sheets-sort.css';
import '@univerjs/presets/lib/styles/preset-sheets-data-validation.css';
import {
  createUniver,
  defaultTheme,
  LocaleType,
  LogLevel,
  Tools,
  UniverInstanceType,
} from '@univerjs/presets';

import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import sheetsCore from '@univerjs/presets/preset-sheets-core/locales/zh-CN';

import { UniverSheetsAdvancedPreset } from '@univerjs/presets/preset-sheets-advanced';
import sheetsAdvanced from '@univerjs/presets/preset-sheets-advanced/locales/zh-CN';

import { UniverSheetsCollaborationPreset } from '@univerjs/presets/preset-sheets-collaboration';
import sheetsCollaboration from '@univerjs/presets/preset-sheets-collaboration/locales/zh-CN';

import { UniverSheetsThreadCommentPreset } from '@univerjs/presets/preset-sheets-thread-comment';
import sheetsThreadComment from '@univerjs/presets/preset-sheets-thread-comment/locales/zh-CN';

import { UniverSheetsConditionalFormattingPreset } from '@univerjs/presets/preset-sheets-conditional-formatting';
import sheetsConditionalFormatting from '@univerjs/presets/preset-sheets-conditional-formatting/locales/zh-CN';

import { UniverSheetsDataValidationPreset } from '@univerjs/presets/preset-sheets-data-validation';
import sheetsDataValidation from '@univerjs/presets/preset-sheets-data-validation/locales/zh-CN';

import { UniverSheetsDrawingPreset } from '@univerjs/presets/preset-sheets-drawing';
import sheetsDrawing from '@univerjs/presets/preset-sheets-drawing/locales/zh-CN';

import { UniverSheetsFilterPreset } from '@univerjs/presets/preset-sheets-filter';
import sheetsFilter from '@univerjs/presets/preset-sheets-filter/locales/zh-CN';

import { UniverSheetsFindReplacePreset } from '@univerjs/presets/preset-sheets-find-replace';
import sheetsFindReplace from '@univerjs/presets/preset-sheets-find-replace/locales/zh-CN';

import { UniverSheetsHyperLinkPreset } from '@univerjs/presets/preset-sheets-hyper-link';
import sheetsHyperLink from '@univerjs/presets/preset-sheets-hyper-link/locales/zh-CN';

import { UniverSheetsSortPreset } from '@univerjs/presets/preset-sheets-sort';
import sheetsSort from '@univerjs/presets/preset-sheets-sort/locales/zh-CN';

// import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
// import sheetsZenEditor from '@univerjs/sheets-zen-editor/locale/zh-CN';
import { UniverSheetsCrosshairHighlightPlugin } from '@univerjs/sheets-crosshair-highlight';
import UniverSheetsCrosshairHighlight from '@univerjs/sheets-crosshair-highlight/locale/zh-CN';

export function setupUniver(
  containerId: string,
  config: {
    header?: boolean;
    footer?: boolean;
    data?: any;
    dataValidationConfig?: any;
    menu?: any;
  } = {
    header: true,
    footer: true,
    data: {},
    dataValidationConfig: {
      showEditOnDropdown: false,
    },
    menu: {},
  }
) {
  const universerEndpoint = window.location.host;

  const collaboration = undefined;

  const { univerAPI, univer } = createUniver({
    locale: LocaleType.ZH_CN,
    locales: {
      [LocaleType.ZH_CN]: Tools.deepMerge(
        {},
        sheetsCore,
        sheetsAdvanced,
        sheetsCollaboration,
        sheetsThreadComment,
        sheetsConditionalFormatting,
        sheetsDataValidation,
        sheetsDrawing,
        sheetsFilter,
        sheetsFindReplace,
        sheetsHyperLink,
        sheetsSort,
        // sheetsZenEditor,
        UniverSheetsCrosshairHighlight
      ),
    },
    collaboration,
    logLevel: LogLevel.VERBOSE,
    theme: defaultTheme,
    presets: [
      UniverSheetsCorePreset({
        container: containerId,
        header: config.header,
        footer: config.footer,
        menu: {
          'sheet.command.add-range-protection-from-toolbar': {
            hidden: true,
          },
          'thread-comment-ui.operation.toggle-panel': {
            hidden: true,
          },
          'sheets-exchange-client.operation.exchange': {
            hidden: true,
          },
          'sheet.operation.open-pivot-table-range-selector-panel': {
            hidden: true,
          },
          'sheet.operation.print-open': {
            hidden: true,
          },
          'sheet.contextMenu.permission': {
            hidden: true,
          },
          'sheets.operation.show-comment-modal': {
            hidden: true,
          },
          ...config.menu,
        },
      }),
      UniverSheetsDrawingPreset({
        collaboration,
      }),
      UniverSheetsAdvancedPreset({
        useWorker: false,
        // if univer page is not in the same domain as the server, you need to set the following parameters
        universerEndpoint,
        // if you want to use the no-limit business feature, you can get 30-day trial license from https://univer.ai/license
        license: process.env.UNIVER_CLIENT_LICENSE,
      }),
      ...(collaboration ? [UniverSheetsCollaborationPreset()] : []),
      UniverSheetsThreadCommentPreset({
        collaboration,
      }),
      UniverSheetsConditionalFormattingPreset(),
      UniverSheetsDataValidationPreset(config.dataValidationConfig),
      UniverSheetsFilterPreset(),
      UniverSheetsFindReplacePreset(),
      UniverSheetsSortPreset(),
      UniverSheetsHyperLinkPreset(),
    ],
    // UniverSheetsZenEditorPlugin
    plugins: [UniverSheetsCrosshairHighlightPlugin],
  });

  univer.createUnit(UniverInstanceType.UNIVER_SHEET, config.data);

  return {
    univerAPI,
    univer,
  };
}
